/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.logging.util;

import cn.hutool.core.util.RandomUtil;

import java.util.Random;

/**
 * <b>(RandomDataUtil)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/11/30
 */
@SuppressWarnings({"unused", "SpellCheckingInspection"})
public class RandomDataUtil {
    private static final String[] EMAIL_SUFFIX    = "@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@live.com,@qq.com,@163.com,@googlemail.com,@126.com,@sina.com,@sohu.com".split(",");
    /**
     * 返回手机号码
     */
    private static final String[] TEL_FIRST       = "131,132,133,134,135,136,137,138,139,145,147,150,151,152,153,155,156,157,158,159,173,175,177,176,178,180,181,182,183,184,185,186,187,188,189".split(",");
    private static final String[] BANK_NAME       = "Industrial and Commercial Bank of China#Agricultural Bank of China#Bank of China#China Construction Bank#Bank of Communications#China Merchants Bank Co., Ltd.#Shanghai Pudong Development Bank#China CITIC Bank#China Everbright Bank CO., Ltd.#China Minsheng Banking Co., Ltd.#Huaxia Bank#Guangdong Development Bank#Shenzhen Development Bank#Industrial Bank Co., Ltd.#China Development Bank#Export-Import Bank of China#Agricultural Development Bank of China".split("#");
    private static final String[] CARD_PRE        = "6,8,9".split(",");
    public static        String   BASE_CHAR       = "abcdefghijklmnopqrstuvwxyz";
    public static        String   BASE_CHAR_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static        String   BASE_NUMBER     = "0123456789";
    public static        String   BASE_SYMBOL     = "~!@#$%^&*_+`-=:;'?,./";
    private static       int      i               = 0;

    /**
     * 随机数
     *
     * @param place 定义随机数的位数
     *
     * @return String
     */
    public static String randomGen(int place) {
        String        base = BASE_CHAR + BASE_CHAR_UPPER + BASE_NUMBER;
        StringBuilder sb   = new StringBuilder();
        Random        rd   = new Random();
        for (int i = 0; i < place; i++) {
            sb.append(base.charAt(rd.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 随机盐值
     *
     * @param place 定义随机数的位数
     *
     * @return String
     */
    public static String randomSalt(int place) {
        String        base = BASE_CHAR + BASE_CHAR_UPPER + BASE_NUMBER + BASE_SYMBOL;
        StringBuilder sb   = new StringBuilder();
        Random        rd   = new Random();
        for (int i = 0; i < place; i++) {
            sb.append(base.charAt(rd.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 返回Email
     *
     * @return String
     */
    public static String getEmail() {
        int           length = getNum(8, 10);
        int           len    = 4;
        StringBuilder sb     = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int number = (int) (Math.random() * BASE_CHAR.length());
            sb.append(BASE_CHAR.charAt(number));
        }
        for (int i = 0; i < length - len; i++) {
            int number = (int) (Math.random() * BASE_NUMBER.length());
            sb.append(BASE_NUMBER.charAt(number));
        }
        sb.append(EMAIL_SUFFIX[(int) (Math.random() * EMAIL_SUFFIX.length)]);
        return sb.toString();
    }

    /**
     * 返回手机号码
     * 中国电信 2G/3G号段：133，153， 180，181，189 4G号段：173， 177 中国联通
     * * 2G/3G号段：130，131，132，155，156，185，186 3G上网卡：145 4G号段：176，185 中国移动
     * * 2G/3G号段：134，135，136，137，138，139，150，151，152，158，159，182，183，184 3G上网卡：147
     * * 4G号段：178，184
     *
     * @return String
     */
    public static String getTel() {
        int    index  = getNum(0, TEL_FIRST.length - 1);
        String first  = TEL_FIRST[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String thrid  = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        return first + second + thrid;
    }

    /**
     * 随机获取银行名称
     *
     * @return String
     */
    public static String getBankName() {
        int index = getNum(0, BANK_NAME.length - 1);
        return BANK_NAME[index];
    }

    /**
     * 返回随机卡号
     *
     * @return String
     */
    public static String getBankNumber() {
        String prefix = CARD_PRE[getNum(0, CARD_PRE.length - 1)];
        String st     = "622" + prefix + getUnixTime();
        return st + getBankCardCheckCode(st);
    }


    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    public static String getNumStr(int start, int end) {
        return String.valueOf(getNum(start, end));
    }

    public static String getAlphabet(String alphabet) {
        return RandomUtil.randomString(alphabet, 1);
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId String
     *
     * @return String
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().isEmpty()
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs     = nonCheckCodeCardId.trim().toCharArray();
        int    luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /***
     * 获取当前系统时间戳 并截取
     *
     * @return String
     */
    private static String getUnixTime() {
        i++;
        i = i > 100 ? i % 10 : i;
        return ((System.currentTimeMillis() / 100) + "").substring(1)
                + (i % 10);
    }

}